import tkinter
from mainPage import *
from project_final.service.user_camera2 import use_camera_register, use_camera_login
from project_final.dao.user_dao import check_username, close_db, check_pwd, insert_username_pwd


class LoginPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量root
        self.root.geometry('%dx%d' % (800, 400))  # 设置窗口大小
        self.username = StringVar()
        self.password = StringVar()
        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)  # 创建Frame
        # global photo
        self.photo = PhotoImage(file='static_img/background.gif')

        Label(self.page).grid(row=0, stick=W)
        Label(self.page, text='账户: ').grid(row=1, column=1, pady=10)
        Entry(self.page, textvariable=self.username).grid(row=1, column=2)
        Label(self.page, text='密码: ').grid(row=2, column=1, pady=10)
        Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=2)
        Button(self.page, text='登陆', command=self.loginCheck).grid(row=3, column=1)
        Button(self.page, text='退出', command=self.quit).grid(row=3, column=3)
        Button(self.page, text='注册', command=self.register).grid(row=3, column=2, pady=10)
        Button(self.page, text='人脸登录', command=self.face_login).grid(row=4, column=2)
        Label(self.page, justify=LEFT, image=self.photo, compound=CENTER, font=("华文行楷", 20),
              fg="black").grid(row=5, columnspan=5)  # 前景色
        self.page.pack()

    def loginCheck(self):
        name = self.username.get()
        pwd = self.password.get()
        flag = check_pwd(name, pwd)
        if flag == 1:
            self.page.destroy()
            MainPage(self.root)
        else:
            showinfo(title='错误', message='账号或密码错误！')

    def register(self):
        username = self.username.get()
        pwd = self.password.get()
        if username != "" and pwd != "":
            if check_username(username) == 1:
                flag = insert_username_pwd(username, pwd)
                if flag == 1:
                    showinfo(title='提示', message='账号注册成功！')
                else:
                    showinfo(title='提示', message='账号注册失败！')
            else:
                showinfo(title='提示', message='用户名重复！')
        else:
            showinfo(title='提示', message='账户密码不能为空')

    def face_login(self):
        self.page.destroy()
        faceLogin(self.root)

    def quit(self):
        close_db()
        self.page.quit()


class faceLogin(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量root
        self.root.geometry('%dx%d' % (300, 200))  # 设置窗口大小
        self.username = StringVar()
        self.password = StringVar()
        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)  # 创建Frame
        self.page.pack()
        Label(self.page).grid(row=0)
        Label(self.page, text='账户: ').grid(row=1, pady=30)
        Entry(self.page, textvariable=self.username).grid(row=1, column=1)
        Button(self.page, text='登陆', command=self.login_check).grid(row=3)
        Button(self.page, text='注册', command=self.register).grid(row=3, column=1, sticky=S)
        Button(self.page, text='返回', command=self.back).grid(row=3, column=1, sticky=E)

    def login_check(self):
        username = self.username.get()
        if check_username(username) == 0:
            flag = use_camera_login(username)
            if flag == 1:
                self.page.destroy()
                MainPage(self.root)
            elif flag == 2:
                tkinter.messagebox.showinfo(title='提示', message="人脸不匹配")
            elif flag == 3:
                tkinter.messagebox.showinfo(title='提示', message="请正确拍摄人像")
            else:
                tkinter.messagebox.showinfo(title='提示', message="您未设置人像登录")
        else:
            tkinter.messagebox.showinfo(title='提示', message="没有该用户名")

    def register(self):
        username = self.username.get()
        if username != "":
            flag = check_username(username)
            if flag == 1:
                msg = use_camera_register(username)
                tkinter.messagebox.showinfo(title='提示', message=msg)
            elif flag == 0:
                tkinter.messagebox.showinfo(title='提示', message="用户名重复")
        else:
            tkinter.messagebox.showinfo(title='提示', message="请先输入用户名")

    def back(self):
        self.page.destroy()
        LoginPage(self.root)
